package com.wangc.report.service.impl;

import com.wangc.report.dao.ConfigMapper;
import com.wangc.report.dao.TragetTableMapper;
import com.wangc.report.entity.ConfigEntity;
import com.wangc.report.entity.DownloadEntity;
import com.wangc.report.entity.TragetTableEntity;
import com.wangc.report.service.ExportToExceService;
import com.wangc.report.utils.RRException;
import org.apache.poi.xssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service("exportToExceService")
public class ExportToExceServiceImpl implements ExportToExceService {
    private static final Logger LOG = LoggerFactory.getLogger(ExportToExceServiceImpl.class);
    @Resource
    private TragetTableMapper tragetTableMapper;
    @Resource
    private ConfigMapper configMapper;

    @Override
    public void export(String tableId, String dataDt) {
        LOG.info("tableId={} dataDt={} ", tableId, dataDt);
        List<TragetTableEntity> tragetTableEntityList = tragetTableMapper.selectByTableDt(tableId, dataDt);
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet();
        XSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 11);
        font.setFontName("宋体");
        //全局样式
        XSSFCellStyle style = wb.createCellStyle();
        style.setFont(font);

        XSSFCellStyle XSSFCellStyleFalse = wb.createCellStyle();
        //XSSFCellStyleFalse.setFillBackgroundColor(XSSFColor.toXSSFColor(HSSFColor.YELLOW));
        XSSFCellStyleFalse.setFont(font);
        int i = -1;
        XSSFRow row = null;
        for (TragetTableEntity tragetTableEntity : tragetTableEntityList) {
            if (i != tragetTableEntity.getRowNo()) {
                row = sheet.createRow(tragetTableEntity.getRowNo());
            }
            i = tragetTableEntity.getRowNo();
            XSSFCell cell1 = row.createCell((tragetTableEntity.getColSeqNo() - 1) * 3);
            cell1.setCellValue(tragetTableEntity.getOldData());
            cell1.setCellStyle(style);
            XSSFCell cell2 = row.createCell((tragetTableEntity.getColSeqNo() - 1) * 3 + 1);
            cell2.setCellValue(tragetTableEntity.getNewData());
            cell2.setCellStyle(style);
            XSSFCell cell3 = row.createCell((tragetTableEntity.getColSeqNo() - 1) * 3 + 2);
            cell3.setCellValue(tragetTableEntity.getCheckResult());
            cell3.setCellStyle(style);
            if (i != 0 && i != 1 && !"true".equals(tragetTableEntity.getCheckResult())) {
                cell3.setCellStyle(XSSFCellStyleFalse);
            }
        }
        ConfigEntity configEntity = configMapper.selectByTableId(tableId);

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(configEntity.getFilePath() + tableId + "_" + dataDt + ".xlsx");
            wb.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
            new RRException(e.toString());
        } finally {
            try {
                fos.close();
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
                new RRException(e.toString());
            }
        }


    }

    @Override
    public List<DownloadEntity> queryList(Map<String, Object> map) {
        return tragetTableMapper.queryGenerateList(map);
    }
}
